﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Manages a single Image
**/

package com.alexrindo.flashview.image 
{
	import com.alexrindo.flashview.layout.LayoutManager;
	import com.alexrindo.flashview.preloader.Preloader;
	import com.alexrindo.flashview.tooltip.Tooltip;
	import com.alexrindo.flashview.UI.zoomSlider;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	public class Image extends Sprite
	{
		private const FRAME_WIDTH:int = 6
		private const FRAME_OFFSET:int = 20
		
		private var _imageLoader:Loader
		protected var _imageHolder:Sprite
		protected var _imageName:String
		protected var _imageType:String
		protected var _imageSize:Number
		protected var _imageHeight:Number
		protected var _imageWidth:Number
		protected var _layout:LayoutManager
		protected var _url:String
		protected var _loaded:Boolean
		
		private var _preloader:Preloader
		
		public static const IMAGE_LOADED:String = "image_loaded"
		
		public function Image(url:String) 
		{
			_url = url
			
			_imageHolder = new Sprite()
			
			loadImage(_url)
			setImageData()
			setPosition()
		}
		
		protected function setPosition():void
		{
			_layout = LayoutManager.getInstance()
			_layout.addStageObject(this, { xDistance: 0, yDistance: 0 }, LayoutManager.CENTER)
		}
		
		protected function setImageData():void
		{
			_imageName = _url.split("/")[1].split(".")[0]
			_imageType = _url.split(".")[1]
		}
		
		public function loadImage(url:String):void
		{
			_imageLoader = new Loader()
			_imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onImageProgress)
			_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded)
			_imageLoader.load(new URLRequest(url))
		}
		
		private function onImageProgress(e:ProgressEvent):void
		{
			_imageSize = Math.round(e.bytesTotal / 102.4) / 10
			dispatchEvent(e)
		}
		
		private function onImageLoaded(e:Event):void
		{
			_loaded = true
			
			_imageHolder.addChild(new Bitmap(e.target.content.bitmapData, "auto", true))
			_imageHolder.alpha = 0 
			_imageHeight = _imageHolder.height
			_imageWidth = _imageHolder.width
			_imageHolder.x = -(imageWidth / 2) + (_imageHolder.width*(scaleX-1))/2
			_imageHolder.y = -(imageHeight / 2) + (_imageHolder.height*(scaleY-1))/2
			addChild(_imageHolder)
			
			dispatchEvent(new Event(IMAGE_LOADED))
		}
		
		public function stopLoad():void
		{
			_imageLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onImageProgress)
		}
		
		public function fadeIn():void
		{
			TweenLite.to(_imageHolder, 0.8, {alpha:1, ease:Circ.easeOut})
		}
		
		public function get loaded():Boolean 
		{ 
			return _loaded
		}
		
		public function resetScale():void
		{
			scaleX = scaleY = 1
		}
		
		public function get imageHeight():Number
		{
			return _imageHeight * scaleY
		}
		
		public function get imageWidth():Number
		{
			return _imageWidth * scaleX
		}
		
		public function get realHeight():Number
		{
			return _imageHeight
		}
		
		public function get realWidth():Number
		{
			return _imageWidth
		}
		
		public function get imageURL():String
		{
			return _url
		}
		
		public function get size():Number
		{
			return _imageSize
		}
		
		public function get type():String
		{
			return _imageType
		}
		
		override public function set scaleX(value:Number):void 
		{
			if (value < 0.5)
			{
				value = 0.5
			} 
			else if (value > 2.5) 
			{
				value = 2.5
			}
			super.scaleX = value
		}
		
		override public function set scaleY(value:Number):void 
		{
			if (value < 0.5)
			{
				value = 0.5
			} 
			else if (value > 2.5) 
			{
				value = 2.5
			}
			super.scaleY = value
		}
	}
}


